<template>
  <div
    class="m-dialog--mask"
    v-if="propVisible"
  >
    <div
      class="m-dialog"
      @click.stop
      :style="{width: width + 'px'}"
    >
      <div
        v-if="showHeader"
        class="m-dialog__header"
      >
        <div class="m-dialog__header-title">{{title}}</div>
        <i
          @click="close"
          class="m-dialog__header-close iconfont iconfont-4"
        ></i>
      </div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "Dialog",
  props: {
    title: {
      type: String,
      default: ""
    },
    width: {
      type: Number,
      default: 470
    },
    visible: {
      type: Boolean,
      default: false
    },
    showHeader: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    visible(v) {
      this.propVisible = v;
    }
  },
  data() {
    return {
      propVisible: this.visible
    };
  },
  methods: {
    close() {
      this.propVisible = false;
      this.$emit("update:visible", false);
      this.$emit("close", false);
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/assets/scss/color";
.m-dialog {
  background: $--background-high;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 4px;
  overflow: hidden;

  &--mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    background: $--mask-background;
  }

  &__header {
    position: relative;
    height: 48px;
    border-bottom: 1px solid $--border-major;

    &::before {
      position: absolute;
      content: "";
      top: 0;
      left: 0;
      right: 0;
      height: 4px;
      background: $--text-high;
    }

    &-title {
      text-align: center;
      line-height: 48px;
      font-weight: 500;
      font-size: 16px;
      color: $--text-major;
    }

    &-close {
      position: absolute;
      top: 0;
      right: 0;
      width: 48px;
      height: 48px;
      line-height: 48px;
      text-align: center;

      &:hover {
        color: $--background-high;
        background: $--text-high;
        cursor: pointer;
      }
    }
  }
}
</style>